// 链接服务器

const db = wx.cloud.database();
const _ = db.command;

var like = "/asstess/like.png";
var like_tap = "/asstess/likeTap.png";
var input_comment_text = "";
var bh_id;
var appUser = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    name: "",
    picture_img: "",
    rank: "1",
    address: "山西省太原市",
    money: "4000",
    contact_num: 0,
    look_num: 0,
    introduce_text: "",
    intorduce_img: [],
    like_img: like,
    praise_img: "/asstess/praise.png",
    //评论：
    comment_area: [{
      user_img: "/asstess/1.jpg",
      user_name: "user",
      comment_text: "",
    }],
    phone: "12345678910",
    // 软键盘
    showInput: false, //控制输入栏
  },
  onLoad: function (bh_int) {
    bh_id = bh_int;
    //浏览量+1
    db.collection('beadhouselist').where({
      id: bh_int.id
    }).update({
      data: {
        look: _.inc(1),
      }
    })
    //获取数据
    db.collection("beadhouselist").where({
      id: bh_int.id
    }).get({
      success: res => {
        res = res.data[0]
        this.setData({
          money: res.money,
          address: res.detail,
          introduce_text: res.text,
          name: res.name,
          address: res.place,
          look_num: res.look,
          picture_img: res.picture,
          contact_num: res.contact.contactNum,
          intorduce_img: res.intorduceImg,
          comment_area: res.comment
        })
      }
    })
    //是否已经收藏
    db.collection('user2').where({
      avatarUrl: this.data.user_img,
      contactBh: [bh_id.id]
    }).get({
      success: res => {
        if (res.data.length)
          this.setData({
            like_img: like_tap
          })
      }
    })
    console.log(appUser.globalData.userImg)
  },
  onShow() {
    console.log(appUser.globalData.userImg)
    this.setData({
      user_img: appUser.globalData.userImg,
      user_name: appUser.globalData.userName
    });

  },

  //收藏点击变色
  like_img_tap() {
    if (appUser.globalData.userImg == null) {
      wx.showToast({
        icon: 'error',
        title: '请先登录'
      })
    } else {
      if (this.data.like_img == "/asstess/like.png") {
        //收藏量+1
        //console.log(this.data.look_num)
        db.collection('beadhouselist').where({
          id: bh_id.id
        }).update({
          data: {
            contact: {
              contactNum: _.inc(1),
            }
          }
        })
        db.collection('user2').where({
          avatarUrl: this.data.user_img
        }).update({
          data: {
            contactBh: _.push(bh_id.id)
          }
        })
        this.setData({
          like_img: like_tap
        })
        wx.showToast({
          icon: 'none',
          title: '已收藏'
        })
      } else {
        //收藏量-1
        //console.log(this.data.look_num)
        db.collection('beadhouselist').where({
          id: bh_id.id
        }).update({
          data: {
            contact: {
              contactNum: _.inc(-1),
            }
          }
        })

        db.collection('user2').where({
          avatarUrl: this.data.user_img
        }).update({
          data: {
            contactBh: _.pull(bh_id.id)
          }
        })
        this.setData({
          like_img: like
        })
        wx.showToast({
          icon: 'none',
          title: '取消收藏'
        })
      }
    }
  },
  //打电话
  goto_phone() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone,
    })
  },
  //软键盘
  goback() {
    if (appUser.globalData.userImg == null) {
      wx.showToast({
        icon: 'error',
        title: '请先登录'
      })
    } else {
      wx.navigateBack({
        success(res) {
          console.log("返回成功")
        },
        fail(res) {
          console.log("error")
        }
      })
    }
  },
  //获取已登录的用户信息
  showInput() {
    if (appUser.globalData.userImg == null) {
      wx.showToast({
        icon: 'error',
        title: '请先登录'
      })
    } else {
    this.setData({
      showInput: true
    })
  }
  },
  //隐藏输入框
  onHideInput: function () {
    this.setData({
      showInput: false
    })
  },
  bindInputMsg(txt) {
    console.log(txt.detail.value);
    input_comment_text = txt.detail.value;
  },
  //留言功能
  comment_goto() {
      var push_obj = {
        user_img: this.data.user_img,
        user_name: this.data.user_name,
        comment_text: input_comment_text,
      };
      this.data.comment_area.push(push_obj);
      this.setData({
        comment_area: this.data.comment_area
      })
      console.log(bh_id)
      db.collection("beadhouselist").where({
        id: bh_id.id
      }).update({
        data: {
          comment: _.push(push_obj)
        }
      })
      wx.showToast({
        title: '发表评论成功',
      })
    
  },
  //预约服务
  goto_res() {
    if (appUser.globalData.userImg == null) {
      wx.showToast({
        icon: 'error',
        title: '请先登录'
      })
    } else {
      wx.navigateTo({
        url: '../beadhouse_res/beadhouse_res?id=' + bh_id.id
      })
    }
  }
})